﻿using Microsoft.Extensions.DependencyInjection;
using WT.OpenIddict.Domain.Shared.LifeTime;

namespace WT.OpenIddict.Domain.Services;

public class SeedDataBaseFactory : ITransientDependency
{
    private readonly IServiceScopeFactory _scopeFactory;

    public SeedDataBaseFactory(IServiceScopeFactory scopeFactory)
    {
        _scopeFactory = scopeFactory;
    }

    public ISeedData Create(string seedDataName)
    {
        using var scope = _scopeFactory.CreateScope();
        var seedDataService = scope.ServiceProvider.GetRequiredService<IList<ISeedData>>();
        ISeedData seedData = null;
        foreach (var item in seedDataService)
            if (item.SeedDataName == seedDataName)
            {
                seedData = item;
                break;
            }

        return seedData;
    }
}